<?php
// application/forms/UserRegister.php
/**
 * Form_UserRegister
 *
 * This form is created in traditional zend style
 *
 * @author Adam Pilacki
 * @version 1.0.0
 */
final class Form_UserRegister extends Zend_Form
{
    public final function init()
    {
        $this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/user/create')
            ->setMethod(Zend_Form::METHOD_POST)
            ->setName('UserRegister')
//            ->setEnctype(Zend_Form::ENCTYPE_MULTIPART)
//            ->setAttrib('onSubmit', 'return validate(this)')
            ->setIsArray(true)
        ;

        $translator = Zend_Registry::get('translator');

        $regexp = '[a-zA-Z0-9_-]+';

        /* login field */
        $loginValidatorNotEmpty = new Zend_Validate_NotEmpty();
        $loginValidatorNotEmpty->setMessage($translator->translate('Login nie może być pusty'));
        $loginValidatorRegex = new Zend_Validate_Regex('/^' . $regexp . '$/i');
        $loginValidatorRegex->setMessage($translator->translate('Wpisany login zawiera niedozwolone znaki'));
        $login = new Zend_Form_Element_Text('login');
        $login->setLabel('login')
//            ->setDescription($translator->translate('Podaj login'))
            ->setValue('')
            ->setRequired(true)
            ->setDecorators(array(
                array('Errors', array('tag' => 'div', 'class' => 'form_errors')),
                array('ViewHelper'),
//                array('Description', array('tag' => 'p', 'class' => 'form_description')),
                array('HtmlTag', array('tag' => 'dd', 'class' => 'form_htmltag')),
                array('Label', array('tag' => 'dt', 'class' => 'form_label')),
            ))
            ->addValidator($loginValidatorNotEmpty, true)
            ->addValidator($loginValidatorRegex)
        ;

        /* password field */
        $passwordValidatorNotEmpty = new Zend_Validate_NotEmpty();
        $passwordValidatorNotEmpty->setMessage($translator->translate('Hasło nie może być puste'));
        $passwordValidatorRegex = new Zend_Validate_Regex('/^' . $regexp . '$/i');
        $passwordValidatorRegex->setMessage($translator->translate('Podane hasło zawiera niedozwolone znaki'));
        $password = new Zend_Form_Element_Password('password');
        $password->setLabel($translator->translate('Hasło'))
            ->setValue('')
            ->setRequired(true)
            ->setDecorators(array(
                array('Errors', array('tag' => 'div', 'class' => 'form_errors')),
                array('ViewHelper'),
//                array('Description', array('tag' => 'p', 'class' => 'form_description')),
                array('HtmlTag', array('tag' => 'dd', 'class' => 'form_htmltag')),
                array('Label', array('tag' => 'dt', 'class' => 'form_label')),
            ))
            ->addValidator($passwordValidatorNotEmpty, true)
            ->addValidator($passwordValidatorRegex)
        ;

        /* password confirm field */
        $passwordConfirmValidatorNotEmpty = new Zend_Validate_NotEmpty();
        $passwordConfirmValidatorNotEmpty->setMessage($translator->translate('Potwierdzenie hasła nie może być puste'));
        $passwordConfirmValidatorRegex = new Zend_Validate_Regex('/^' . $regexp . '$/i');
        $passwordConfirmValidatorRegex->setMessage($translator->translate('Potwierdzenie hasła zawiera niedozwolone znaki'));
        $passwordConfirm = new Zend_Form_Element_Password('passwordConfirm');
        $passwordConfirm->setLabel($translator->translate('Potwierdzenie hasła'))
            ->setValue('')
            ->setRequired(true)
            ->setDecorators(array(
                array('Errors', array('tag' => 'div', 'class' => 'form_errors')),
                array('ViewHelper'),
//                array('Description', array('tag' => 'p', 'class' => 'form_description')),
                array('HtmlTag', array('tag' => 'dd', 'class' => 'form_htmltag')),
                array('Label', array('tag' => 'dt', 'class' => 'form_label')),
            ))
            ->addValidator($passwordConfirmValidatorNotEmpty, true)
            ->addValidator($passwordConfirmValidatorRegex)
        ;

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($translator->translate('Zarejestruj się'));

        $this->addElements(array($login, $password, $passwordConfirm, $submit));
    }
}